package com.hcy.app.domain

import android.util.Log
import com.koushikdutta.async.http.body.MultipartFormDataBody
import com.koushikdutta.async.http.body.MultipartFormDataBody.MultipartCallback
import com.koushikdutta.async.http.body.Part
import com.koushikdutta.async.http.server.AsyncHttpServer
import com.koushikdutta.async.http.server.AsyncHttpServerRequest
import com.koushikdutta.async.http.server.AsyncHttpServerResponse
import com.koushikdutta.async.http.server.HttpServerRequestCallback
import com.koushikdutta.async.stream.OutputStreamDataCallback
import org.json.JSONObject
import java.io.FileOutputStream

class FileUploadServer(
    private val port: Int,
    private val uploadDirectory: String
) {
    private val server = AsyncHttpServer()

    fun start() {
        // 主页 - 文件上传表单
        server.get("/", HttpServerRequestCallback { request, response ->
            val html = """
                <!DOCTYPE html>
                <html>
                <head>
                    <title>文件上传</title>
                    <meta name="viewport" content="width=device-width, initial-scale=1">
                    <style>
                        body { font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; }
                        h1 { color: #333; }
                        form { margin-top: 20px; }
                        input[type="file"] { margin-bottom: 10px; }
                        button { background-color: #4CAF50; color: white; padding: 10px 15px; border: none; border-radius: 4px; cursor: pointer; }
                        button:hover { background-color: #45a049; }
                    </style>
                </head>
                <body>
                    <h1>上传文件到Android设备</h1>
                    <form action="/upload" method="post" enctype="multipart/form-data">
                        <input type="file" name="file" required>
                        <button type="submit">上传</button>
                    </form>
                </body>
                </html>
            """.trimIndent()
            response.headers.set("Content-Type", "text/html; charset=utf-8")
            response.send(html)
        })

        server.post("/upload", object : HttpServerRequestCallback {
            override fun onRequest(
                request: AsyncHttpServerRequest,
                response: AsyncHttpServerResponse
            ) {
                val body=request.getBody<MultipartFormDataBody>()
                body?.run {
                    multipartCallback = object :MultipartCallback{
                        override fun onPart(part: Part?) {
                            val filename = part?.filename.toString()
                            OutputStreamDataCallback(FileOutputStream("$uploadDirectory/$filename"))
                            response.send(JSONObject().apply { put("file",filename) })
                        }
                    }
                }
            }
        })
        server.listen(port)
    }

    fun stop() {
        server.stop()
    }

    companion object{
        private const val TAG = "FileUploadServer"
    }
}
